/****************************************************************************
**
** Trolltech hereby grants a license to use the Qt/Eclipse Integration
** plug-in (the software contained herein), in binary form, solely for the
** purpose of creating code to be used with Trolltech's Qt software.
**
** Qt Designer is licensed under the terms of the GNU General Public
** License versions 2.0 and 3.0 ("GPL License"). Trolltech offers users the
** right to use certain no GPL licensed software under the terms of its GPL
** Exception version 1.2 (http://trolltech.com/products/qt/gplexception).
**
** THIS SOFTWARE IS PROVIDED BY TROLLTECH AND ITS CONTRIBUTORS (IF ANY) "AS
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
**
** Since we now have the GPL exception I think that the "special exception
** is no longer needed. The license text proposed above (other than the
** special exception portion of it) is the BSD license and we have added
** the BSD license as a permissible license under the exception.
**
****************************************************************************/


#include <QPushButton>
#include <QWidget>
#include <QPushButton>
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <qgridlayout.h>
#include <QNetworkProxy>
#include <qsettings>
#include <QMessageBox>
#include <qsettings.h>
#include <QListWidget>
#include <QListWidgetItem>
#include <qlabel.h>
#include <QPalette>
#include <QStandardItemModel>
#include <QListView>
#include <QListWidgetItem>
#include "QGoogleCalendar.h"
#include "QCalParseEventDetailsXML.h"
#include "QCalendarView.h"
#include "QCalLoginWidget.h"
#include "QCalEventList.h"
QGoogleCalendar::QGoogleCalendar(QWidget *parent)
    : QMainWindow(parent)
{

	setContextMenuPolicy(Qt::NoContextMenu);
	// Create stacked widget
	stackedWidget = new QStackedWidget(this);
	stackedWidget->setContextMenuPolicy(Qt::NoContextMenu);

	// Start listening current stacked widget changes
	QObject::connect(stackedWidget, SIGNAL(currentChanged(int)),this, SLOT(activeWidgetChanged(int)));
	
	// Create first active widget
	CalenderWidget = new QCalendarView(stackedWidget); 
	  
	// Add QStackedWidget as a central widget of QMainWindow
	setCentralWidget(stackedWidget);
	
	login();
	//ui.setupUi(this);
}

void QGoogleCalendar::login()
{
QSettings setting("Nokia", "QTnetworkGUI");
if(setting.contains("Name"))
	{
	QString name(setting.value("Name").toString());
	QString	pass(setting.value("pass").toString());
	QString url="https://www.google.com/calendar/feeds/default/private/full";
	evntRetr = new QCalEventRetriever;
	evntRetr->load(url);
	QObject::connect(evntRetr, SIGNAL(downloadCompleted()),this, SLOT(launchXmlParser()));
	}
else
	{
	QCalLoginWidget *LWidget = new QCalLoginWidget(stackedWidget);
	QObject::connect(LWidget, SIGNAL(logincomplete()),this, SLOT(login()));
	//activateNewWidget(LWidget);
	stackedWidget->addWidget(LWidget);
	stackedWidget->setCurrentWidget(LWidget);
	stackedWidget->show();	
	}

}

void QGoogleCalendar::launchXmlParser()
{
	QFile file("c:\\data\\PrivateEvent.xml");
	file.open(QIODevice::ReadOnly);
	
	xmlInput=new QCalParseEventDetailsXML();
	xmlInput->parseXML(file);
	eventArr = xmlInput->geteventDetailsArray();
	CalenderWidget->updatevents(eventArr);
	QObject::connect(CalenderWidget, SIGNAL(clicked(QDate)),this, SLOT(eventlistview(QDate)));
	file.close();
	
}

void QGoogleCalendar::eventlistview(QDate dateSelected)
{
	QVector<eventDetail> dayevents;
	QVector<QString> eventTime;

	eventArr=xmlInput->geteventDetailsArray();
	for(TInt Count=0;Count<eventArr.count();Count++)
		{
		eventDetail eventSelected = eventArr.at(Count);
		QString event = eventSelected.startTime;		
		QString year =event.left(event.indexOf("-"));
		event = event.right(event.length()-event.indexOf("-")-1);
		QString month =event.left(event.indexOf("-"));
		event = event.right(event.length()-event.indexOf("-")-1);
		QString day =event.left(event.indexOf("T"));
		bool error;
		TInt Iyear =year.toInt(&error,10);
		TInt Imonth =month.toInt(&error,10);
		TInt Iday =day.toInt(&error,10);
		
		
		QDate date(Iyear,Imonth,Iday);
		if(dateSelected == date)//&& Imonth ==calendar->monthShown()
			{
			dayevents.append(eventArr.at(Count));			
			
			}
		}
	if(dayevents.count()!=0)
		{
		QString strerr;
	    strerr=QString("Event No.:%1")
	    		.arg(dayevents.count());
	    QMessageBox msgBoxErr;
	     msgBoxErr.setText(strerr);
	     msgBoxErr.exec();
		//QMyWidget2* newWidget = new QMyWidget2(stackedWidget,dayevents/*,eventTime*/);
		QCalEventList* newWidget = new QCalEventList(stackedWidget,dayevents/*,eventTime*/);
		//stackedWidget->removeWidget(this);
		//activateNewWidget(newWidget);
		stackedWidget->addWidget(newWidget);
		stackedWidget->setCurrentWidget(newWidget);
		stackedWidget->show();	
		
		}
}
QGoogleCalendar::~QGoogleCalendar()
{

}
//----------------------------------------------------------------------------------------------------

/*
QLoginWidget::QLoginWidget(QStackedWidget* stackedWid, QWidget *parent)
//: QWidget(stackedWidget,parent)
	{
	//createMenus();
	stackedWidget=stackedWid;
	QSettings setting("Nokia", "QTnetworkGUI");
	
	pWidget = new QWidget();
	
	name=new QLineEdit("abc@gmail.com");
	pass=new QLineEdit("password");
		//pass->setEchoMode(QLineEdit::Password);

	QPushButton* dnldButton = new QPushButton("&Login", this);
	QPushButton* nxtButton = new QPushButton("&Next Page", this);
	QObject::connect(dnldButton, SIGNAL(clicked()),
		                  this, SLOT(login()));
	QObject::connect(nxtButton, SIGNAL(clicked()),
			                  this, SLOT(nxtPage()));
	
	QVBoxLayout* layout = new QVBoxLayout();
	layout->addWidget(name);
	layout->addWidget(pass);
	layout->addWidget(dnldButton);
	layout->addWidget(nxtButton);
	this->setLayout(layout);
	//pWidget->showMaximized();
	
//	stackedWidget->addWidget(pWidget);
//	pWidget->show();
	
	authRetr = NULL; 
	evntRetr = NULL;
	}

QLoginWidget::~QLoginWidget()
	{
	//delete menuBar;
	if(authRetr)
	delete authRetr;
	}


//void QLoginWidget::createMenus()
//    {
//    
//    }

//QMenuBar* QLoginWidget::MenuBar() const
//    {
//    return menuBar;
//    }


void QLoginWidget::login()
	{
	

	QString url="https://www.google.com/accounts/ClientLogin";	
	QString name_fld=name->text();
	QString pass_fld=pass->text();
//	setting.setValue("Name",name_fld);
//	setting.setValue("Pass",pass_fld);
	authRetr= new QCalAuthRetriever;
	authRetr->load(url,name_fld,pass_fld);
	QObject::connect(authRetr, SIGNAL(downloadCompleted()),this, SLOT(ActivateCalender()));
	
//	upldr = new uploader;
//	upldr->load(url);
	}
void QLoginWidget::ActivateCalender()
	{
	
	stackedWidget->removeWidget(this);
	stackedWidget->setCurrentIndex(0);
	stackedWidget->show();
	emit logincomplete();
	}

void QLoginWidget::eventlistview(QDate date)
	{
//	QMyWidget2* newWidget = new QMyWidget2(stackedWidget,date);
//	stackedWidget->removeWidget(pWidget);
//	activateNewWidget(newWidget);
//	stackedWidget->addWidget(newWidget);
//	newWidget->show();
	}


*/

//---------------------------------------------------------------------------------------------------
/*

QMyWidget2::QMyWidget2(QStackedWidget* stackedWid,QVector<eventDetail>& dayEvents,QWidget *parent)
    //: QBaseWidget(stackedWidget,parent)
    {
	
	
    stackedWidget=stackedWid;
    eventDetails = dayEvents;
    listWidget = new QListWidget();
   	for(int i=0;i<dayEvents.count();i++)
   		{
		lst = new QListWidgetItem(dayEvents.at(i).title+"\n"+dayEvents.at(i).startTime, listWidget);
   //		itemArr.append(lst);
   		listWidget->addItem(lst);
   		
   		}
   	QPushButton* nxtButton = new QPushButton("&Prev Page", this);
   	QObject::connect(nxtButton, SIGNAL(clicked()),
   				                  this, SLOT(PrevPage()));
   
   QObject::connect(listWidget, SIGNAL(clicked(const QModelIndex &)),
    		                  this, SLOT(index(const QModelIndex &)));
   layout = new QVBoxLayout(); 
   layout->addWidget(listWidget);
    layout->addWidget(nxtButton);
    
    this->setLayout(layout);
   

    }
void QMyWidget2::PrevPage()
	{
	//stackedWidget->currentWidget()->setHidden(true);
	stackedWidget->removeWidget(this);//todo:delete newwidget
	stackedWidget->setCurrentIndex(0);
	stackedWidget->show();
	
//	//QMyWidget2* newWidget = new QMyWidget2(stackedWidget,eventArray,this);
//	QGoogleCalendar* newWidget=new QGoogleCalendar(this);
//	stackedWidget->removeWidget(this);
//	activateNewWidget(newWidget);
//	stackedWidget->addWidget(newWidget);
//	newWidget->show();
	}

void QMyWidget2::index(const QModelIndex & in)
	{
	int row=in.row();
	NxtPage(eventDetails.at(row));
	}
QMyWidget2::~QMyWidget2()
    {
  if(listWidget)
	  delete listWidget;
//  if(lst)
//	  delete lst;
//  if(layout)
//	  delete layout;
    }

//void QMyWidget2::createMenus()
//    {
//     Create menubar for this widget
//    // We sets these menu items into into QMainWindow menu when this widget is active
//    menuBar = new QMenuBar();
//
//    menuAction = new QAction("Hello QMyWidget2",menuBar);
//    menuBar->addAction(menuAction);
//    connect(menuAction, SIGNAL(triggered()),this, SLOT(hello()));
//    }
//
//QMenuBar* QMyWidget2::MenuBar() const
//    {
//  //  return menuBar;
//    }

void QMyWidget2::NxtPage(eventDetail event)
	{

	QDetailsWidget* newWidget = new QDetailsWidget(stackedWidget,event,listWidget);
	
	stackedWidget->removeWidget(listWidget);
	//delete listWidget;
	//activateNewWidget(newWidget);	
	stackedWidget->addWidget(newWidget);	
	stackedWidget->setCurrentWidget(newWidget);
	stackedWidget->show();
	}
*/

//-----------------------------------------------------------------------------------------------

/*QDetailsWidget::QDetailsWidget(QStackedWidget* stackedWid,eventDetail& event, QWidget *parent)
   // : QBaseWidget(stackedWidget,parent)
	{
	QMessageBox msgBoxErr;
	 msgBoxErr.setText("QDetailsWidget");
	 msgBoxErr.exec();
	stackedWidget=stackedWid;
	//rWidget = new QWidget();
	QString strTitle;
	QString strContent;
	QString strWhere;
	QString strWhenEnd;
	QString strWhenStart;
	strTitle.append(event.title);
	strContent.append(event.content);
	strWhere.append(event.location);
	strWhenEnd.append(event.endTime);
	strWhenStart.append(event.startTime);
	QTextEdit* text= new QTextEdit(this);
	text->clear();
	text->append("======Title======");
	text->append(strTitle);
	text->append("");
	text->append("======Content======");
	text->append(strContent);
	text->append("");
	text->append("======Place======");
	text->append(strWhere);
	text->append("");
	text->append("======StartTime======");
	text->append(strWhenStart);
	text->append("");
	text->append("======EndTime======");
	text->append(strWhenEnd);
	text->append("");
	text->setReadOnly(true);
	QPushButton* dnldButton = new QPushButton("&NextPage", this);
	QPushButton* nxtButton = new QPushButton("&Prev Page", this);
	QObject::connect(nxtButton, SIGNAL(clicked()),
			                  this, SLOT(PrevPage()));
	QVBoxLayout* layout = new QVBoxLayout();
	layout->addWidget(text);
	layout->addWidget(dnldButton);
	layout->addWidget(nxtButton);
	this->setLayout(layout);
//	this->repaint();
//	this->show();
	
	//pWidget->showMaximized();
	
//	stackedWidget->addWidget(this);
//	this->showMaximized();
	}

QDetailsWidget::~QDetailsWidget()
    {
  //  delete menuBar;
  //  delete upldr;
    }


void QDetailsWidget::NxtPage()
	{
	
	}
void QDetailsWidget::PrevPage()
	{
	//stackedWidget->currentWidget()->setHidden(true);
	stackedWidget->removeWidget(this);//todo:delete newwidget
	stackedWidget->setCurrentIndex(0);
	stackedWidget->show();
	
//	//QMyWidget2* newWidget = new QMyWidget2(stackedWidget,eventArray,this);
//	QGoogleCalendar* newWidget=new QGoogleCalendar(this);
//	stackedWidget->removeWidget(this);
//	activateNewWidget(newWidget);
//	stackedWidget->addWidget(newWidget);
//	newWidget->show();
	}*/
/*eventDetails::eventDetails()
	{
	
	}
eventDetails::~eventDetails()
	{
	
	}*/
